<?php

namespace App\Controllers;

 class Travel extends BaseController
{
    public function create($id = NULL) 
    { 
        $fields = [ 
            'title' => 'Create a Destination', 
            'heading' => 'Create a Destination', 
            'footer' => 'Copyright xieyuhang' 
        ]; 
 
        $record = [ 
            'id' => 0, 
            'name' => '', 
            'description' => '', 
            'link' => '', 
            'image' => '', 
        ]; 
 
        $parser = \Config\Services::parser(); 
        return $parser->setData($fields) 
            ->render('templates\top') . 
            $parser->setData($record) 
                ->render('editplace') . 
            $parser->setData($fields) 
                ->render('templates\bottom'); 
    }
    public function delete($id)
{
    $places = new \App\Models\Places();
    
    // 检查记录是否存在
    if (!$places->find($id)) {
        throw new \CodeIgniter\Exceptions\PageNotFoundException("Place with ID $id not found.");
    }

    // 删除记录
    $places->delete($id);

    // 重定向回列表页或显示成功消息
    return redirect()->to('/travel')->with('message', 'Destination deleted successfully.');
}
    public function handle() {
        $places = new \App\Models\Places();
        helper(['form', 'url']);
        $id = $this->request->getPost('id');
        $record = $places->find($id); 
        if ($this->validate($places->validationRules)) { 
            if ($id == 0) { 
                $places->save([ 
               'name' => $this->request->getPost('name'), 
               'description' => $this->request->getPost('description'), 
               'link' => $this->request->getPost('link'), 
               'image' => $this->request->getPost('image'), 
               ]); 
              } else { 
                $places->save([ 
                 'id' => $this->request->getPost('id'), 
                 'name' => $this->request->getPost('name'), 
                 'description' => $this->request->getPost('description'), 
                 'link' => $this->request->getPost('link'), 
                 'image' => $this->request->getPost('image'), 
                ]); 
             }

             }
             $fields = [
                 'title' => 'Edit a Destination',
                 'heading' => 'Edit a Destination',
                 'footer' => 'Copyright xieyuhang'
                 ];
                 $parser = \Config\Services::parser();
                 $returnView = $parser->setData($fields)->render('templates\top');
                 if (!$this->validate($places->validationRules)) {
                     $returnView .= $parser->renderString($this->validator->listErrors());
                     $returnView .= $parser->setData($this->request->getPost())
                       ->render('editplace');
                 } else {
                     $returnView .= $parser->renderString("<p>Success!</p>");
                     $returnView .= $parser->setData($this->request->getPost())
                        ->render('editplace');
                 }
                $returnView .= $parser->setData($fields)
                    ->render('templates\bottom');
                   return $returnView;
             }
      

    public function index()
    {
        helper('form');
         // connect to the model
        $places = new \App\Models\Places();
         // retrieve all the records
        $records = $places->findAll();

        $table = new \CodeIgniter\View\Table();
        $headings = $places->db->getFieldNames('places');
$displayHeadings = array_slice($headings, 1, 2);
$table->setHeading(array_map('ucfirst', $displayHeadings));
foreach ($records as $record) { 
    $nameLink = anchor("travel/showme/" . $record['id'], $record['name']); 
    $deleteForm = form_open("/travel/delete/{$record['id']}", ['method' => 'post']);
    $deleteButton = '<button type="submit" class="btn btn-danger btn-sm">Delete</button>';
    $deleteForm .= $deleteButton;
    $deleteForm .= form_close();

    // 添加描述和删除按钮到表格
    $table->addRow($nameLink, $record['description'], $deleteForm);
}


    $template = [
        'table_open' => '<table cellpadding="5px">',
        'cell_start' => '<td style="border: 1px solid #dddddd;">',
        'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];
        $table->setTemplate($template);

        $fields = [
            'title' => 'Travel Destinations',
            'heading' => 'Travel Destinations',
            'footer' => 'Copyright xieyuhang'
        ];

        

        
        // get a template parser
        $parser = \Config\Services::parser();


        return $parser->setData($fields)
        ->render('templates\top') .
 $table->generate() .
 $parser->setData($fields)
        ->render('templates\bottom');
        // tell it about the substitions
       // return $parser->setData(['places' => $records])
        // and have it render the template with those
        //->render('placeslist');

        
    }

    

    public function showme($id)  
    {  
        // 连接到模型  
        $places = new \App\Models\Places();  
        
        // 根据 ID 获取记录  
        $record = $places->find($id);  
    
        // 检查是否找到记录  
        if (!$record) {  
            throw new \CodeIgniter\Exceptions\PageNotFoundException("Place with ID $id not found.");  
        }  
    
        // 创建一个新的表实例  
        $table = new \CodeIgniter\View\Table();  
        // 设置详细信息表的标题  
        $table->addRow(anchor(uri: "travel/edit/$id", title: "Edit"));
        
        // 添加记录的详细信息到表格  
        $table->addRow('ID', $record['id']);  
        $table->addRow('Name', $record['name']);  
        $table->addRow('Description', $record['description']);  
        $table->addRow('Tourism Site', '<a href="' . $record['link'] . '" target="_blank">' . $record['link'] . '</a>'); // 添加 link  
        $table->addRow('Image', '<img src="/image/' . $record['image'] . '" alt="' . $record['name'] . ' image" style="max: width 400px;px;"/>'); // 添加 image  
    
        // 设置表的模板  
        $template = [  
            'table_open' => '<table cellpadding="5px">',  
            'cell_start' => '<td style="border: 1px solid #dddddd;">',  
            'row_alt_start' => '<tr style="background-color:#dddddd">',  
        ];  
        $table->setTemplate($template);  
    
        // 准备顶部和底部模板的数据  
        $fields = [  
            'title' => 'Travel Destination Details',  
            'heading' => '' . $record['name'],  
            'footer' => 'Copyright xieyuhang'  
        ];  
    
        // 获取模板解析器  
        $parser = \Config\Services::parser();  
    
        // 渲染顶部模板、表格和底部模板  
        return $parser->setData($fields)  
            ->render('templates/top') .  
            $table->generate() .  
            $parser->setData($fields)  
            ->render('templates/bottom');  
    }
        public function edit($id = NULL) { 
        $places = new \App\Models\Places();
        $record = $places->find($id);
       
         $fields = [
         'title' => 'Edit a Destination',
         'heading' => 'Edit a Destination',
         'footer' => 'Copyright xieyuhang'

          ];
        $parser = \Config\Services::parser();
       return $parser->setData($fields)
        ->render('templates\top') .
        $parser->setData($record)
        ->render('editplace') .
        $parser->setData($fields)
        ->render('templates\bottom');


    }
 


}
